

***************************
*  Bootstrapping Program  *
***************************
 
program define debt_boot_A1, rclass
 
	use "File 1 for Bootstrap.dta", clear
	
	bsample, cluster(id)

sort bucket
by bucket: egen count = count(bucket) 

count if ltd_iss_all_other >= 0 
scalar totalcount = r(N)
drop if ltd_iss_all_other > 60000 | ltd_iss_all_other < 1000

collapse (mean) count bucket1-bucket16 bucket_2-bucket_14 Qdummy bucket_Q bucket2_Q bucket3_Q bucket4_Q bucket5_Q bucket6_Q bucket7_Q bucket8_Q bucket9_Q ltd_iss_all_other, by(bucket)

gen logcount = log(count)

reg logcount bucket bucket_2-bucket_8 bucket2-bucket13 if ltd_iss_all_other  > 2000 & ltd_iss_all_other < 40000 

scalar fp = exp((_b[bucket] * 10) + (_b[bucket_2] * 10^2) + (_b[bucket_3] * 10^3) + (_b[bucket_4] * 10^4) + (_b[bucket_5] * 10^5) + (_b[bucket_6] * 10^6) + (_b[bucket_7] * 10^7) + (_b[bucket_8] * 10^8)  + _b[_cons]) * exp((e(rmse)^2/2))
gen pcount = 0
replace pcount = exp((_b[bucket] * bucket) + (_b[bucket_2] * bucket_2) + (_b[bucket_3] * bucket_3) + (_b[bucket_4] * bucket_4) + (_b[bucket_5] * bucket_5) + (_b[bucket_6] * bucket_6) + (_b[bucket_7] * bucket_7) + (_b[bucket_8] * bucket_8) + _b[_cons]) * exp((e(rmse)^2/2)) 

* counts from counterfactual *
sum pcount if ltd_iss_all_other > 8500 & ltd_iss_all_other <= 10000
scalar cfbcount = r(sum)
sum pcount if ltd_iss_all_other > 10000 & ltd_iss_all_other <= 14500 
scalar cfacount  = r(sum)
sum pcount if ltd_iss_all_other > 8500 & ltd_iss_all_other <= 14500 
scalar cfcount = r(sum)

* actual counts  * 
sum count if ltd_iss_all_other > 8500 & ltd_iss_all_other <= 10000 
scalar actbcount = r(sum)
sum count if ltd_iss_all_other > 10000 & ltd_iss_all_other <= 14500
scalar actacount = r(sum)
sum count if ltd_iss_all_other > 8500 & ltd_iss_all_other <= 14500 
scalar actcount = r(sum)

* size of excess mass
return scalar inranmanip_ex = (actbcount - cfbcount) / cfacount

end



foreach year in 1987 1992 1997 2002 2007 2012 {

use "Data/Census.dta", clear

keep if year == `year'

gen bucket = .
 forvalues x = 1000(500)60000 {
 replace bucket = `x' if ltd_iss_all_other > `x' - 500 & ltd_iss_all_other <= `x' 
 }
 
replace bucket = bucket/1000
 
forvalues y = 2/14 {
gen bucket_`y' = bucket^`y'
}

gen Qdummy = ltd_iss_all_other <= 10000
gen bucket_Q = bucket * Qdummy

forvalues y = 2/14 {
gen bucket`y'_Q = bucket_`y' * Qdummy
}

gen bucket_alt = round((bucket/0.5) - 16)
forvalues q = 1(1)16 {
gen bucket`q' = bucket_alt == `q'
}

drop bucket_alt
keep ltd_iss_all_other id bucket* type_code Qdummy year

save "File 1 for Bootstrap.dta", replace

sort bucket
by bucket: egen count = count(bucket) 
count if ltd_iss_all_other >= 0
scalar totalcount = r(N)
 
drop if ltd_iss_all_other > 60000 | ltd_iss_all_other < 1000

collapse (mean) count bucket1-bucket16 bucket_2-bucket_14 Qdummy bucket_Q bucket2_Q bucket3_Q bucket4_Q bucket5_Q bucket6_Q bucket7_Q bucket8_Q bucket9_Q ltd_iss_all_other, by(bucket)

gen logcount = log(count)

reg logcount bucket bucket_2-bucket_8 bucket2-bucket13 if ltd_iss_all_other  > 2000 & ltd_iss_all_other < 40000 

scalar fp = exp((_b[bucket] * 10) + (_b[bucket_2] * 10^2) + (_b[bucket_3] * 10^3) + (_b[bucket_4] * 10^4) + (_b[bucket_5] * 10^5) + (_b[bucket_6] * 10^6) + (_b[bucket_7] * 10^7) + (_b[bucket_8] * 10^8)  + _b[_cons]) * exp((e(rmse)^2/2))
gen pcount = 0
replace pcount = exp((_b[bucket] * bucket) + (_b[bucket_2] * bucket_2) + (_b[bucket_3] * bucket_3) + (_b[bucket_4] * bucket_4) + (_b[bucket_5] * bucket_5) + (_b[bucket_6] * bucket_6) + (_b[bucket_7] * bucket_7) + (_b[bucket_8] * bucket_8) + _b[_cons]) * exp((e(rmse)^2/2)) 


* counts from counterfactual *
sum pcount if ltd_iss_all_other > 8500 & ltd_iss_all_other <= 10000 
scalar cfbcount = r(sum)
sum pcount if ltd_iss_all_other > 10000 & ltd_iss_all_other <= 14500 
scalar cfacount  = r(sum)
sum pcount if ltd_iss_all_other > 8500 & ltd_iss_all_other <= 14500 
scalar cfcount = r(sum)


* actual counts  * 
sum count if ltd_iss_all_other > 8500 & ltd_iss_all_other <= 10000 
scalar actbcount = r(sum)
sum count if ltd_iss_all_other > 10000 & ltd_iss_all_other <= 14500
scalar actacount = r(sum)
sum count if ltd_iss_all_other > 8500 & ltd_iss_all_other <= 14500 
scalar actcount = r(sum)

* size of excess mass
local inranmanip_ex = (actbcount - cfbcount) / cfacount

simulate inranmanip_ex = ((actbcount - cfbcount) / cfacount), reps(500) seed(20316): debt_boot_A1

* Standard Errors
reg inranmanip_ex
sum inranmanip_ex
local inranmanip_ex_se = r(sd)

outreg2 using "Table A1", append adds(Excess Mass, `inranmanip_ex', Excess Mass_SE, `inranmanip_ex_se') nocons nor2 auto(2) nonotes noobs 
}

